package com.intellagric.service.impl.module.sys_office;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.SysOfficeMapper;
import com.intellagric.pojo.SysOffice;
import com.intellagric.pojo.SysOfficeExample;
import com.intellagric.service.module.sys_office.OfficeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/12
 * @DESC： 组织机构管理服务实现
 */
@Service
public class OfficeServiceImpl implements OfficeService {

    @Autowired
    private SysOfficeMapper sysOfficeMapper;

    @Override
    public ResponseMessage addOffice(SysOffice sysOffice) {
        if (sysOfficeMapper.insertSelective(sysOffice) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    @Override
    public ResponseMessage deleteOffice(String id) {
        if (sysOfficeMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    @Override
    public ResponseMessage editOffice(SysOffice sysOffice) {
        if (sysOfficeMapper.updateByPrimaryKeySelective(sysOffice) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    @Override
    public SysOffice getOfficeById(String id) {
        return sysOfficeMapper.selectByPrimaryKey(id);
    }

    @Override
    public LayuiDataGridResult getOfficeByParentId(String id) {
        SysOfficeExample sysOfficeExample = new SysOfficeExample();
        sysOfficeExample.setOrderByClause("sort DESC");//以sort字段进行排序，倒序
        sysOfficeExample.createCriteria().andParentIdEqualTo(id);
        List<SysOffice> officeList = sysOfficeMapper.selectByExample(sysOfficeExample);
        return LayuiDataGridResult.success().add(officeList,officeList.size());
    }

    @Override
    public LayuiDataGridResult getOfficeList() {
        SysOfficeExample sysOfficeExample  = new SysOfficeExample();
        sysOfficeExample.createCriteria().andIdIsNotNull();
        List<SysOffice> officeList = sysOfficeMapper.selectByExample(sysOfficeExample);
        return LayuiDataGridResult.success().add(officeList,officeList.size());
    }

    @Override
    public LayuiDataGridResult getOfficeTree() {
        //查询所有列表数据
        LayuiDataGridResult result = new LayuiDataGridResult();
        List<SysOffice> officeList = this.getOfficeList().getData();
        result.setData(officeList);

        //查询顶级列表
        List<SysOffice> rootOfficeList = this.getOfficeByParentId("0").getData();

        //生成树形数据，为一级菜单设置子菜单，getChild是递归调用的
        for (SysOffice sysOffice : rootOfficeList) {
            sysOffice.setChildren(getChild(sysOffice.getId(), officeList));
        }
        result.setData(rootOfficeList);
        return result;
    }

    private List<SysOffice> getChild(String id,List<SysOffice> officeList) {
        // 子列表
        List<SysOffice> childList = new ArrayList<>();

        // 遍历所有节点，将父列表id与传过来的id比较
        for (SysOffice sysOffice : officeList) {
            if (sysOffice.getParentId() != null) {   //所有非一级列表的都要加入chidList中（递归）
                if (sysOffice.getParentId().equals(id)) {
                    childList.add(sysOffice);
                }
            }
        }

        // 把子列表的子子列表再循环一遍
        for (SysOffice sysOffice : childList) {// 没有url子列表还有子列表
            // 递归
            sysOffice.setChildren(getChild(sysOffice.getId(), officeList));
        } // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }


}
